package com.dingding.special.user.account.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.dingding.common.core.base.dao.impl.BaseDaoImpl;
import com.dingding.common.core.base.daox.ICriteriaBaseDao;
import com.dingding.common.core.beans.special.Specialuseraccount;
import com.dingding.common.core.beans.special.Specialuserlogin;
import com.dingding.special.user.account.dao.ISpecialUserAccountDao;
import com.dingding.special.user.account.dto.ResUserPacketDTO;
import com.dingding.special.user.account.sql.UserAccountSQL;

/**
 * 
 * @ClassName: SpecialUserAccountDaoImpl 
 * @Description: 专车司机账户DAO实现类 
 * @author YHQ
 * @date 2016年8月19日 上午9:58:08
 * @since V2.4.0
 * 
 */
@Component("specialUserAccountDao")
public class SpecialUserAccountDaoImpl extends BaseDaoImpl<Specialuseraccount> implements ISpecialUserAccountDao{

	private Logger log = Logger.getLogger(this.getClass());
	
	@Resource(name = "criteriaBaseDao")
	private ICriteriaBaseDao criteriaBaseDao;
	
	/**
	 * 
	 * @Title: getSpecialUserAccount 
	 * @Description: 根据用户Id获取用户账户表
	 * @param userId 用户Id
	 * @author YHQ 
	 * @since V2.4.0
	 * @return Specialuseraccount    返回类型
	 * 
	 */
	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public Specialuseraccount getSpecialUserAccount(String userId) {
		
		Criterion criterion = Restrictions.eq("userId", userId);
		
		try {
			
			return super.queryCriteriaUnResult(criterion);
		} catch (Exception e) {
			log.error("根据用户Id获取用户账户表时出现异常   类："+this.getClass()+" getSpecialUserAccount()", e);
		}
		
		return null;
	}

	/**
	 * 
	 * @Title: gePacketInfo 
	 * @Description: 获取用户钱包信息
	 * @param userId 获取用户Id
	 * @author YHQ 
	 * @since V2.4.0 
	 * @return ResUserPacketDTO    返回类型
	 * 
	 */
	@SuppressWarnings("unchecked")
	@Override
	@Transactional(readOnly=true)
	public ResUserPacketDTO gePacketInfo(String userId) {
		
		try {
			
			// 获取用户钱包信息
			List<ResUserPacketDTO> resUserPacketDTOs = (List<ResUserPacketDTO>) super.listSqlFreeObject(UserAccountSQL.GET_USER_PACKET, ResUserPacketDTO.class, userId);
			
			if(resUserPacketDTOs != null && resUserPacketDTOs.size() > 0){
				resUserPacketDTOs.get(0).setCurrencyAmountString(resUserPacketDTOs.get(0).getCurrencyAmount().toString());
				return resUserPacketDTOs.get(0);
			} 
			
		} catch (Exception e) {
			log.error("获取用户钱包信息时出现异常   类："+this.getClass()+" gePacketInfo()", e);
		}
		
		return null;
	}

	/**
	 * 
	 * @Title: getSpecialUserLogin 
	 * @Description: 根据用户Id获取用户登录信息
	 * @param userId 用户Id
	 * @author YHQ 
	 * @since V2.4.0
	 * @return Specialuserlogin    返回类型
	 * 
	 */
	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public Specialuserlogin getSpecialUserLogin(String userId) {
		
		Criterion criterion = Restrictions.eq("suid", userId);
		
		try {
			return criteriaBaseDao.queryCriteriaListUnique(Specialuserlogin.class, criterion);
		} catch (Exception e) {
			log.error("根据用户id获取用户登录信息时出现异常   类：" + this.getClass() + " getSpecialUserLogin", e);
		}
		
		return null;
	}

}
